<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+

require_once 'eleontev/Util/Replacer.php';
require_once 'eleontev/HTML/tplTemplatez.php';
require_once 'core/app/AppMsg.php';
require_once 'core/app/AppMailModel.php';


class AppMailParser
{

	var $template_dir;
	var $templates = array('send_to_friend'			=> 'kb_friend.txt',
	                       'answer_to_user'			=> 'kb_answer.txt',
						   'contact'				=> 'kb_contact.txt',
						   'confirm_registration'	=> 'confirm_registration.txt',
						   'generated_password'		=> 'generated_password.txt',
						   
						   'admin_generated_password'		=> 'admin_generated_password.txt',
						   //'admin_confirm_registration'	=> 'admin_confirm_registration.txt'
						   );
	
	
	var $vars      = array('support_name', 
		                   'support_email', 
						   'support_mailer',
						   'name',
						   'username',
						   'first_name',
						   'last_name',
						   'midlle_name',
						   'email',
						   'link'
					       );

	
	
	function AppMailParser() {
		$this->template_dir = APP_EMAI_TMPL_DIR;
		$this->setReplaser();
	}

	
	function setReplaser() {
		$this->replacer = &new Replacer;
		$this->replacer->s_var_tag = "[";
		$this->replacer->e_var_tag = "]";
	}
	
	
	function setSettingVars($setting) {
		$this->assign('support_name', $setting['from_name']);
		$this->assign('support_email', $setting['from_email']);
		$this->assign('support_mailer', $setting['from_mailer']);
	}
	
	
	//function setNames($values) {
	//	if(!empty($values['first_name'] && !empty($values['last_name'])) {
	//		$this->assign('name', $values['first_name'] . ' ' . $values['first_name']);
	//	}
	//}
	
	
	function getVars() {
		return $this->vars;
	}
	
	
	function assign($var, $value = false) {
		$this->replacer->assign($var, $value);
	}
	
	
	function parse($str) {
		return $this->replacer->parse($str);
	}
	
	
	function setTemplate($key, $value) {
		$this->templates[$key] = $value;
	}
	
	
	function getTemplateMsg($letter_key = false) {
		return AppMsg::getMsg('template_msg.ini', 'email_setting', $letter_key);
	}
	
	
	function getTemplate($letter_key) {
		
		$msg = $this->getTemplateMsg($letter_key);
		$template = $this->template_dir . $this->templates[$letter_key];
		
		$tpl = &new tplTemplatez($template);
		$tpl->clean_html = false;
		$tpl->tplParse($msg);
		return $tpl->tplPrint(1);
	}
}
?>